/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.dt.core.refer;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataReference;
import com.inspur.edp.lcm.metadata.api.entity.uri.MetadataURI;
import com.inspur.edp.lcm.metadata.api.service.RefCommonService;
import com.inspur.edp.lcm.metadata.spi.IMetadataReferenceManager;
import com.inspur.edp.sgf.api.entity.SgMetadata;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: EapiRefConstraint
 * @Author: Fynn Qi
 * @Date: 2021/1/4 10:54
 * @Version: V1.0
 */
public class EapiRefConstraint implements IMetadataReferenceManager {

    private static final String EAPI_RESOURCE_TYPE_VO = "VO";

    private static final String EAPI_RESOURCE_TYPE_VO_SIMPLE = "VO-Simple";

    private static final String VO_TYPE = "GSPViewModel";

    @Override
    public List<MetadataReference> getConstraint(GspMetadata gspMetadata) {
        if (gspMetadata == null || gspMetadata.getContent() == null) {
            return null;
        }
        SgMetadata eapi = (SgMetadata) gspMetadata.getContent();
        if (eapi.getService() == null
                || StringUtils.isBlank(eapi.getService().getResourceId())) {
            return null;
        }
        List<MetadataReference> metadataRefs = new ArrayList<>();
        if (EAPI_RESOURCE_TYPE_VO.equals(eapi.getService().getResourceType())
                || EAPI_RESOURCE_TYPE_VO_SIMPLE.equals(eapi.getService().getResourceType())) {
            MetadataReference reference = new MetadataReference();
            reference.setMetadata(gspMetadata.getHeader());
            GspMetadata voMetadata = this.getResourceMetadata(gspMetadata);
            reference.setDependentMetadata(voMetadata.getHeader());
            metadataRefs.add(reference);
        }
        return metadataRefs;
    }

    private GspMetadata getResourceMetadata(GspMetadata eapiMetadata) {
        SgMetadata eapi = (SgMetadata) eapiMetadata.getContent();
        MetadataURI targetMdURI = new MetadataURI();
        targetMdURI.setId(eapi.getService().getResourceId());
        targetMdURI.setCode(eapi.getService().getResourceCode());
        targetMdURI.setNameSpace(eapi.getService().getResourceNameSpace());
        if (EAPI_RESOURCE_TYPE_VO.equals(eapi.getService().getResourceType())
                || EAPI_RESOURCE_TYPE_VO_SIMPLE.equals(eapi.getService().getResourceType())) {
            targetMdURI.setType(VO_TYPE);
        }
        MetadataURI sourceMdURI = new MetadataURI();
        sourceMdURI.setId(eapiMetadata.getHeader().getId());
        sourceMdURI.setCode(eapiMetadata.getHeader().getCode());
        sourceMdURI.setType(eapiMetadata.getHeader().getType());
        sourceMdURI.setNameSpace(eapiMetadata.getHeader().getNameSpace());

        GspMetadata resourceMeta = SpringBeanUtils.getBean(RefCommonService.class)
                .getRefMetadata(targetMdURI, sourceMdURI, eapiMetadata.getRelativePath());
        return resourceMeta;
    }


}
